const app = getApp()
Page({
    data:{
        send:false,
        alreadySend:false,
        second:60,
        code:'',
        buttonType:false,
        disabled:true,
        mobile:'',
        address:'',
        face:'../../images/back.png',
        birthday : '',
        real_name : '',
        card : '',
        gender : '',
        nation : '',
        back:'../../images/face.png'
    },
    onLoad(){
      wx.setNavigationBarTitle({
        title: '实名认证'
      })
        wx.setBackgroundColor({
            setBackgroundColor: '#EBEBEB'
        })
    },
    inputPhoneNum(e){
        let phoneNum = e.detail.value
        if (phoneNum.length === 11) {
            let checkedNum = this.checkPhoneNum(phoneNum)
            if (checkedNum) {
                this.setData({
                    mobile: phoneNum
                })
                console.log('phoneNum' + this.data.mobile)
                this.showSendMsg()
                this.activeButton()
            }
        } else {
            this.setData({
                mobile: ''
            })
            this.hideSendMsg()
        }
    },
    inputCode(e){
        let code = e.detail.value
        if (code.length === 6){
            this.setData({
                code:e.detail.value
            })
            this.activeButton()
        }
    },
    checkPhoneNum: function (phoneNum) {
        let str = /^1\d{10}$/
        if (str.test(phoneNum)) {
            return true
        } else {
            wx.showToast({
                title: '手机号不正确',
                image: '../../images/icon/fail.jpg'
            })
            return false
        }
    },
    showSendMsg: function () {
        if (!this.data.alreadySend) {
            this.setData({
                send: true
            })
        }
    },
    hideSendMsg: function () {
        this.setData({
            send: false,
            disabled: true,
            buttonType: false
        })
    },
    sendMsg: function () {
        if (this.data.send){
            wx.request({
                url:app.globalData.msg,
                method:'POST',
                dataType: 'json',
                data:{mobile:this.data.mobile},
                success:response=>{
                    if (response.statusCode===200 && response.data.success){
                        this.setData({
                            alreadySend: true,
                            send: false
                        })
                        this.timer()
                    }else {
                        wx.showToast({
                            title: '手机号不正确',
                            icon:'fail'
                        })
                    }
                }
            })

        }

    },
    timer: function () {
        let promise = new Promise((resolve, reject) => {
            let setTimer = setInterval(
                () => {
                    this.setData({
                        second: this.data.second - 1
                    })
                    if (this.data.second <= 0) {
                        this.setData({
                            second: 60,
                            alreadySend: false,
                            send: true
                        })
                        resolve(setTimer)
                    }
                }
                , 1000)
        })
        promise.then((setTimer) => {
            clearInterval(setTimer)
        })
    },
    onSubmit(){
        if (this.data.buttonType){
            app.axios('/auth','PUT',response=>{
                wx.redirectTo({
                    url: "/pages/authSuccess/index"
                })
            },{
                mobile:this.data.mobile,
                address:this.data.address,
                face:this.data.face,
                birthday:this.data.birthday,
                real_name:this.data.real_name,
                card:this.data.card,
                gender:this.data.gender,
                nation:this.data.nation,
                back:this.data.back,
                code:this.data.code
            })
        }
    },
    addCode(e){

    },
    activeButton(){
        let {mobile, address, face,birthday,real_name,card,gender,nation,back,code} = this.data
        if (mobile && address && face && birthday && real_name && card && gender && nation && back && code ) {
            this.setData({
                buttonType: true
            })
        } else {
            this.setData({
                buttonType: false
            })
        }
    },
    selectImg(e){
        let type = e.currentTarget.dataset.type;
        let face = false;
        let url = app.globalData.upload;
        if (type==='face'){
            url = url + '?type=card&id=1';
            face = !face;
        }else {
            url = url + '?type=card&id=2';
        }
        wx.chooseImage({
            success: res => {
                let tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: url, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'source',
                    success: res=>{
                        // var data = res.data
                        //do something
                        let response = JSON.parse(res.data);
                        console.log(response);
                        if (response.code === 200){
                            let data = response.data
                            if (face) {
                                console.log(res);

                                this.setData({
                                    face : data.file,
                                    address : data.address,
                                    birthday : data.birthday,
                                    real_name : data.name,
                                    card : data.card,
                                    gender : data.gender,
                                    nation : data.nation,
                                })
                            }else {
                                this.setData({
                                    back:data.file
                                })
                            }
                        }

                    }
                })
            }
        })
    }
})
